package cn.lystudio.rohmtimeopwide_background_v11.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.UserInfoDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.UserRoleDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.UsersPO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohmtimeopwide_background_v11.exceptions.JsonException;
import cn.lystudio.rohmtimeopwide_background_v11.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/user")
@CrossOrigin
@Slf4j
public class UserController {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/doLogin")
    public SaResult userLogin(@RequestParam(value = "username") String userCode, @RequestParam("password") String password) {
        UsersPO user = userService.getUserLogin(userCode, password);
        if (ObjectUtil.isNotEmpty(user)) {
            StpUtil.login(user.getUserCode());
        } else {
            if(StpUtil.isLogin()){
                StpUtil.logout();
            }
        }
        return SaResult.data(StpUtil.getTokenInfo());
    }

    @PostMapping("/logout")
    public void logout() {
        if(StpUtil.isLogin()){
            StpUtil.logout();
        }
    }

    @SaCheckLogin
    @GetMapping("/getUser")
    public SaResult getUser(){
        UsersPO user = userService.get1User_Usable(StpUtil.getLoginIdAsString());
        return SaResult.data(user);
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page, Integer limit,@RequestParam(name = "keyword",defaultValue = "")String keyword){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if (roleList.size() != 1) throw new JsonException(500, "权限异常");
        PageUtil.setFirstPageNo(1);
        List<UserInfoDTO> allUserInfo = userService.getAllUserInfo(keyword);
        if(CollUtil.isEmpty(allUserInfo)) return LayuiPageApiResponse.ofSuccessData(null,0);

        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,allUserInfo),allUserInfo.size());
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer userID,Boolean enabled){
        return SaResult.data(userService.uptUserEnabled(userID,enabled));
    }


    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/delUser")
    public SaResult delUser(Integer userID){
        return SaResult.data(userService.delUser(userID));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/addUser")
    public SaResult addUser(@RequestBody UserInfoDTO userInfoDTO){
        return SaResult.data(userService.addUser(userInfoDTO));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptUser")
    public SaResult uptUser(@RequestBody UserInfoDTO userInfoDTO){
        return SaResult.data(userService.uptUser(userInfoDTO));
    }

    @GetMapping("/get1UserRole")
    public SaResult get1UserRole(String userCode){
        UserRoleDTO userRole = userService.get1UserRole_Usable(StpUtil.getLoginIdAsString());
        if(ObjectUtil.isEmpty(userRole)) throw new JsonException(500,"用户及角色状态异常");
        return SaResult.data(userRole);
    }
    @PostMapping("/changePwd")
    public SaResult changePwd(String userCode,String oldPassword,String newPassword){
        int res = userService.uptUserPwd(userCode,oldPassword,newPassword);
        if(res<1) return SaResult.error("修改密码失败");
        return SaResult.ok();
    }

}
